package cn.njyd.corejava.thread.myday04;

import java.util.concurrent.locks.LockSupport;

public class LockSupportDemo01 {
    private static Thread t1,t2;
    private static volatile boolean flag = true;

    public static void main(String[] args) {
        t1 = new Thread(()->{
            for (int i=1;i<=100;i+=2) {
                while(!flag) {
                    LockSupport.park();
                }
                System.out.print(i+" ");
                flag = !flag;
                LockSupport.unpark(t2);
            }
        },"打印奇数线程");
        t2 = new Thread(()->{
            for (int i=2;i<=100;i+=2) {
                while(flag) {
                    LockSupport.park();
                }
                System.out.print(i+" ");
                flag = !flag;
                LockSupport.unpark(t1);
            }
        },"打印偶数线程");
        t1.start();;
        t2.start();

    }
}
